package cn.hznc.controller.service.schedule;

import cn.hutool.core.collection.CollectionUtil;
import cn.hznc.controller.service.toolAnalyze.PersonnelUsageAnalysisService;
import cn.hznc.domain.entity.*;
import cn.hznc.domain.request.toolAnalyze.ToolQuantityMachineToolsReq;
import cn.hznc.mapper.EntryExitRecordMapper;
import cn.hznc.mapper.ICabinetRecordMapper;
import cn.hznc.mapper.ProductionMapper;
import cn.hznc.vo.PersonnelKnifeInstallationVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class ToolSchedulingHomepageService {
    @Resource
    private PersonnelUsageAnalysisService personnelUsageAnalysisService;
    @Resource
    private EntryExitRecordMapper entryExitRecordMapper;
    @Resource
    private ICabinetRecordMapper iCabinetRecordMapper;
    @Resource
    private ProductionMapper productionMapper;

    public List<NameNumberEntity> queryPersonnelKnifeInstallation(){
        List<NameNumberEntity> list = new ArrayList<>();
        LambdaQueryWrapper<ICabinetRecordEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ICabinetRecordEntity::getType,"加料单");
        queryWrapper.ne(ICabinetRecordEntity::getOperator,"");
        List<ICabinetRecordEntity> entryExitRecordEntityList = iCabinetRecordMapper.selectList(queryWrapper);
//        LambdaQueryWrapper<EntryExitRecordEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
//        lambdaQueryWrapper.ne(EntryExitRecordEntity::getOperator,"");
//        List<EntryExitRecordEntity> entryExitRecordEntityList = entryExitRecordMapper.selectList(lambdaQueryWrapper);
        Map<String,List<ICabinetRecordEntity>> map1 = entryExitRecordEntityList.stream().collect(Collectors.groupingBy(ICabinetRecordEntity::getOperator));
        Iterator<Map.Entry<String, List<ICabinetRecordEntity>>> entries = map1.entrySet().iterator();
        while (entries.hasNext()) {
            NameNumberEntity nameNumberEntity = new NameNumberEntity();
            Map.Entry<String, List<ICabinetRecordEntity>> entry = entries.next();
            nameNumberEntity.setName(entry.getKey());
            nameNumberEntity.setNumber(entry.getValue().stream().filter(s -> StringUtils.isNotEmpty(s.getNumber())).mapToInt(c ->Integer.parseInt(c.getNumber())).sum());
            list.add(nameNumberEntity);
//            Map<String, List<EntryExitRecordEntity>> map2 = entry.getValue().stream().collect(Collectors.groupingBy(EntryExitRecordEntity::getType));
//            try {
//                nameNumberEntity.setNumber(map2.get("inbound").stream().collect(Collectors.summingInt(EntryExitRecordEntity::getNumber)));

//            } catch (Exception e) {
//                nameNumberEntity.setNumber(0);
//                nameNumberEntity.setName(entry.getKey());
//
//                list.add(nameNumberEntity);
//            }
        }
        return list;
    }
    public List<NameNumberEntity> queryPersonnelKnifeInstallation2(){
        List<NameNumberEntity> list = new ArrayList<>();
        LambdaQueryWrapper<ICabinetRecordEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ICabinetRecordEntity::getType,"取料单");
        queryWrapper.ne(ICabinetRecordEntity::getOperator,"");
        List<ICabinetRecordEntity> entryExitRecordEntityList = iCabinetRecordMapper.selectList(queryWrapper);
//        LambdaQueryWrapper<EntryExitRecordEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
//        lambdaQueryWrapper.ne(EntryExitRecordEntity::getOperator,"");
//        List<EntryExitRecordEntity> entryExitRecordEntityList = entryExitRecordMapper.selectList(lambdaQueryWrapper);
        Map<String,List<ICabinetRecordEntity>> map1 = entryExitRecordEntityList.stream().collect(Collectors.groupingBy(ICabinetRecordEntity::getOperator));
        Iterator<Map.Entry<String, List<ICabinetRecordEntity>>> entries = map1.entrySet().iterator();
        while (entries.hasNext()) {
            NameNumberEntity nameNumberEntity = new NameNumberEntity();
            Map.Entry<String, List<ICabinetRecordEntity>> entry = entries.next();
            nameNumberEntity.setName(entry.getKey());
            nameNumberEntity.setNumber(entry.getValue().stream().filter(s -> StringUtils.isNotEmpty(s.getNumber())).mapToInt(c ->Integer.parseInt(c.getNumber())).sum());
            list.add(nameNumberEntity);
//            Map<String, List<EntryExitRecordEntity>> map2 = entry.getValue().stream().collect(Collectors.groupingBy(EntryExitRecordEntity::getType));
//            try {
//                nameNumberEntity.setNumber(map2.get("inbound").stream().collect(Collectors.summingInt(EntryExitRecordEntity::getNumber)));

//            } catch (Exception e) {
//                nameNumberEntity.setNumber(0);
//                nameNumberEntity.setName(entry.getKey());
//
//                list.add(nameNumberEntity);
//            }
        }
        return list;
    }

    public List<PersonnelKnifeInstallationVo> queryProductKnifeUsage(){
        List<PersonnelKnifeInstallationVo> personnelKnifeInstallationVos = new ArrayList<>();
//        List<NameNumberEntity> list = new ArrayList<>();
        LambdaQueryWrapper<ICabinetRecordEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ICabinetRecordEntity::getType,"取料单");
        queryWrapper.ne(ICabinetRecordEntity::getCraft,"");
        List<ICabinetRecordEntity> entryExitRecordEntityList = iCabinetRecordMapper.selectList(queryWrapper);
//        LambdaQueryWrapper<EntryExitRecordEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
//        lambdaQueryWrapper.ne(EntryExitRecordEntity::getOperator,"");
//        List<EntryExitRecordEntity> entryExitRecordEntityList = entryExitRecordMapper.selectList(lambdaQueryWrapper);
        Map<String,List<ICabinetRecordEntity>> map1 = entryExitRecordEntityList.stream().collect(Collectors.groupingBy(ICabinetRecordEntity::getCraft));
        Map<String,List<ICabinetRecordEntity>> map2 = sortMap(map1);
        Iterator<Map.Entry<String, List<ICabinetRecordEntity>>> entries = map2.entrySet().iterator();
        while (entries.hasNext()) {
            NameNumberEntity nameNumberEntity = new NameNumberEntity();
            Map.Entry<String, List<ICabinetRecordEntity>> entry = entries.next();
//            nameNumberEntity.setName(entry.getKey());
//            nameNumberEntity.setNumber(entry.getValue().stream().filter(s -> StringUtils.isNotEmpty(s.getNumber())).mapToInt(c ->Integer.parseInt(c.getNumber())).sum());
//            list.add(nameNumberEntity);
            PersonnelKnifeInstallationVo personnelKnifeInstallationVo = new PersonnelKnifeInstallationVo();
            LambdaQueryWrapper<ProductionEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(ProductionEntity::getMaterialNo,entry.getKey());
            List<ProductionEntity> productionEntities =  productionMapper.selectList(lambdaQueryWrapper);
            if (CollectionUtil.isNotEmpty(productionEntities))
                personnelKnifeInstallationVo.setList(productionEntities);
            personnelKnifeInstallationVo.setCraft(entry.getKey());
            personnelKnifeInstallationVo.setNumber(entry.getValue().stream().filter(s -> StringUtils.isNotEmpty(s.getNumber())).mapToInt(c ->Integer.parseInt(c.getNumber())).sum());
            personnelKnifeInstallationVos.add(personnelKnifeInstallationVo);
//            Map<String, List<EntryExitRecordEntity>> map2 = entry.getValue().stream().collect(Collectors.groupingBy(EntryExitRecordEntity::getType));
//            try {
//                nameNumberEntity.setNumber(map2.get("inbound").stream().collect(Collectors.summingInt(EntryExitRecordEntity::getNumber)));

//            } catch (Exception e) {
//                nameNumberEntity.setNumber(0);
//                nameNumberEntity.setName(entry.getKey());
//
//                list.add(nameNumberEntity);
//            }
        }
        return personnelKnifeInstallationVos;
    }

    public List<NameNumberEntity> queryToolQuantityMachineTools(ToolQuantityMachineToolsReq toolQuantityMachineToolsReq){
        List<NameNumberEntity> list = new ArrayList<>();
        LambdaQueryWrapper<ICabinetRecordEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ICabinetRecordEntity::getType,"取料单");
        queryWrapper.ne(ICabinetRecordEntity::getMachineTool,"");
        if (CollectionUtil.isNotEmpty(toolQuantityMachineToolsReq.getMachineToolName())){
            queryWrapper.in(ICabinetRecordEntity::getMachineTool,toolQuantityMachineToolsReq.getMachineToolName());
        }
        if (toolQuantityMachineToolsReq.getStartTime() != null&&toolQuantityMachineToolsReq.getEndTime()!= null){
            if (toolQuantityMachineToolsReq.getStartTime().equals(toolQuantityMachineToolsReq.getEndTime())){
                Date date = toolQuantityMachineToolsReq.getEndTime();
                Calendar calendar = new GregorianCalendar();
                calendar.setTime(date);
                calendar.add(Calendar.DATE, 1);
                date = calendar.getTime();
                toolQuantityMachineToolsReq.setEndTime(date);
            }
            queryWrapper.between(ICabinetRecordEntity::getTime,toolQuantityMachineToolsReq.getStartTime(),toolQuantityMachineToolsReq.getEndTime());
        }
        List<ICabinetRecordEntity> entryExitRecordEntityList = iCabinetRecordMapper.selectList(queryWrapper);
//        LambdaQueryWrapper<EntryExitRecordEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
//        lambdaQueryWrapper.ne(EntryExitRecordEntity::getOperator,"");
//        List<EntryExitRecordEntity> entryExitRecordEntityList = entryExitRecordMapper.selectList(lambdaQueryWrapper);
        Map<String,List<ICabinetRecordEntity>> map1 = entryExitRecordEntityList.stream().collect(Collectors.groupingBy(ICabinetRecordEntity::getMachineTool));
        Iterator<Map.Entry<String, List<ICabinetRecordEntity>>> entries = map1.entrySet().iterator();
        while (entries.hasNext()) {
            NameNumberEntity nameNumberEntity = new NameNumberEntity();
            Map.Entry<String, List<ICabinetRecordEntity>> entry = entries.next();
            nameNumberEntity.setName(entry.getKey());
            nameNumberEntity.setNumber(entry.getValue().stream().filter(s -> StringUtils.isNotEmpty(s.getNumber())).mapToInt(c ->Integer.parseInt(c.getNumber())).sum());
            list.add(nameNumberEntity);
//            Map<String, List<EntryExitRecordEntity>> map2 = entry.getValue().stream().collect(Collectors.groupingBy(EntryExitRecordEntity::getType));
//            try {
//                nameNumberEntity.setNumber(map2.get("inbound").stream().collect(Collectors.summingInt(EntryExitRecordEntity::getNumber)));

//            } catch (Exception e) {
//                nameNumberEntity.setNumber(0);
//                nameNumberEntity.setName(entry.getKey());
//
//                list.add(nameNumberEntity);
//            }
        }
        return list;
    }

    public List<String> queryAllMachineTool(){
        LambdaQueryWrapper<ICabinetRecordEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ICabinetRecordEntity::getType,"取料单");
        queryWrapper.ne(ICabinetRecordEntity::getMachineTool,"");
        queryWrapper.groupBy(ICabinetRecordEntity::getMachineTool);
        List<ICabinetRecordEntity> entryExitRecordEntityList = iCabinetRecordMapper.selectList(queryWrapper);
        List<String> strings = new ArrayList<>();
        entryExitRecordEntityList.forEach(iCabinetRecordEntity -> {
            strings.add(iCabinetRecordEntity.getMachineTool());
        });
        return strings;
    }
    private static Map<String, List<ICabinetRecordEntity>> sortMap(Map<String, List<ICabinetRecordEntity>> map) {
        //利用Map的entrySet方法，转化为list进行排序
        List<Map.Entry<String, List<ICabinetRecordEntity>>> entryList = new ArrayList<>(map.entrySet());
        //利用Collections的sort方法对list排序
        Collections.sort(entryList, new Comparator<Map.Entry<String, List<ICabinetRecordEntity>>>() {
            @Override
            public int compare(Map.Entry<String, List<ICabinetRecordEntity>> o1, Map.Entry<String, List<ICabinetRecordEntity>> o2) {
                //正序排列，倒序反过来
                return o2.getValue().stream().filter(s -> StringUtils.isNotEmpty(s.getNumber())).mapToInt(c ->Integer.parseInt(c.getNumber())).sum() - o1.getValue().stream().filter(s -> StringUtils.isNotEmpty(s.getNumber())).mapToInt(c ->Integer.parseInt(c.getNumber())).sum();
            }
        });
        //遍历排序好的list，一定要放进LinkedHashMap，因为只有LinkedHashMap是根据插入顺序进行存储
        LinkedHashMap<String, List<ICabinetRecordEntity>> linkedHashMap = new LinkedHashMap<String, List<ICabinetRecordEntity>>();
        int i = 10;
        for (Map.Entry<String, List<ICabinetRecordEntity>> e : entryList
        ) {
            linkedHashMap.put(e.getKey(),e.getValue());
            i--;
            if (i <= 0){
                break;
            }
        }

        return linkedHashMap;
    }
}
